package com.lss.music.activity;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.android.material.badge.BadgeDrawable;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.lss.music.R;
import com.lss.music.fragment.DiscoveryFragment;
import com.lss.music.fragment.FollowFragment;
import com.lss.music.fragment.MineFragment;
import com.lss.music.fragment.NoteFragment;

public class HomeActivity extends AppCompatActivity {
    private BottomNavigationView bottomNavigationView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_home);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(navListener);
        // 默认显示发现页面
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new DiscoveryFragment()).commit();
        }
        setBadge(R.id.nav_follow, 5);
    }

    private BottomNavigationView.OnNavigationItemSelectedListener navListener = item -> {
        int itemId = item.getItemId();
        if (itemId == R.id.nav_discovery) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    new DiscoveryFragment()).commit();
            return true;
        } else if (itemId == R.id.nav_follow) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    FollowFragment.newInstance()).commit();
            return true;
        } else if (itemId == R.id.nav_note) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    NoteFragment.newInstance()).commit();
            return true;
        } else if (itemId == R.id.nav_mine) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                    MineFragment.newInstance()).commit();
            return true;
        }
        return false;
    };

    /**
     * 设置角标
     *
     * @param menuItemId 菜单项id
     * @param number     数量
     */
    private void setBadge(int menuItemId, int number) {
        BadgeDrawable badge = bottomNavigationView.getOrCreateBadge(menuItemId);
        badge.setVisible(true);
        badge.setNumber(number);
        badge.setBackgroundColor(ContextCompat.getColor(this, menuItemId));
    }
}